<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>机票价格查询</title>
    </head>
    <body>
        <div id='search_div'>
        </div>
        <div id='search_grid_div'>
        </div>

        <script>
            Ext.onReady(function(){
                var airways_code = "";
                var fly_date = "";
                var fromto = "";
                var today = new Date().format('Y-m-d');

                var airportTpl = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{airport_name}({airport_code})', '</div></tpl>');
                var fromAirport = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: "/admin/getAllAirportComboBox"
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, [{
                                name: 'id'
                            }, {
                                name: 'airport_name'
                            }, {
                                name: 'airport_code'
                            }])
                    }),
                    fieldLabel: "始发城市",
                    name: 'from_airport',
                    allowBlank: false,
                    blankText: "始发城市必须选择",
                    emptyText: '拼音首字母/代码',
                    typeAhead: false,
                    loadingText: '正在查找...',
                    hideTrigger: false,
                    tpl: airportTpl,
                    minChars: 0,
                    width: '230',
                    itemSelector: 'div.search-item',
                    onSelect: function(record){
                        fromAirport.setValue(record.data.airport_name);
                        from.setValue(record.data.airport_code);
                        if (this.isExpanded()) {
                            this.collapse();
                        }
                    }
                });
                fromAirport.on("focus",function(){this.selectText();});   
                var toAirport = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: "/admin/getAllAirportComboBox"
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, [{
                                name: 'id'
                            }, {
                                name: 'airport_name'
                            }, {
                                name: 'airport_code'
                            }])
                    }),
                    fieldLabel: "目的城市",
                    name: 'to_airport',
                    allowBlank: false,
                    blankText: "目的城市必须选择",
                    emptyText: '拼音首字母/代码',
                    typeAhead: false,
                    loadingText: '正在查找...',
                    hideTrigger: false,
                    tpl: airportTpl,
                    minChars: 0,
                    width: '230',
                    itemSelector: 'div.search-item',
                    onSelect: function(record){
                        toAirport.setValue(record.data.airport_name);
                        to.setValue(record.data.airport_code);
                        if (this.isExpanded()) {
                            this.collapse();
                        }
                    }
                });
                toAirport.on("focus",function(){this.selectText();}); 
                var from = new Ext.form.Hidden({
                    name: 'from'
                });
                var to = new Ext.form.Hidden({
                    name: 'to'
                });
                var date = new Ext.form.DateField({
                    fieldLabel: "<span class='red'>*</span>始发日期",
                    name: 'date',
                    format: 'Y-m-d', //格式化日期
                    width: 90,
                    invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
                    allowBlank: false,
                    inputType: '2008-01-01',
                    minValue: today,
                    value: today
                });
                var airways = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: '/admin/getInternalAirways'
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root',
                            id: 'id'
                        }, ['id', 'airways_name', 'airways_code'])
                    }),
                    name: 'airways',
                    loadingText: '正在加载...',
                    displayField: 'airways_name',
                    valueField: 'airways_code',
                    mode: 'remote',
                    triggerAction: 'all',
                    hiddenName: 'airways',
                    fieldLabel: "航空公司",
                    emptyText: '所有',
                    editable: false,
                    allowBlank: true,
                    width: 60,
                    maxHeight: 150,
                    tabIndex: 4
                });
                var personNum = new Ext.form.ComboBox({ //combobox
                    name: 'person_number',
                    fieldLabel: '乘客人数',
                    resiable: true,
                    typeAhead: true,
                    handleHeight: 100,
                    displayField: 'value',
                    valueField: 'value',
                    selectOnFocus: true,
                    mode: 'local',
                    width: 60,
                    triggerAction: 'all',//这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
                    readOnly: true,
                    value: "1",
                    store: new Ext.data.Store({
                        reader: new Ext.data.ArrayReader({}, ['value']),
                        data: [['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]
                    })
                });
                var searchButton = new Ext.Button({
                    text: '搜 索',
                    handler: searchFlights
                })

                var formPanel = new Ext.form.FormPanel({
                    //baseCls: 'x-plain',
                    labelWidth: 65,
                    labelAlign: 'right',
                    width: '860',
                    frame: true,
                    items: [{
                            layout: 'column',
                            border: false,
                            items: [{
                                    columnWidth: .35,
                                    layout: 'form',
                                    border: false,
                                    items: [fromAirport]
                                }, {
                                    columnWidth: .2,
                                    layout: 'form',
                                    border: false,
                                    items: [date]
                                }, {
                                    columnWidth: .17,
                                    layout: 'form',
                                    border: false,
                                    items: [airways]
                                }, {
                                    columnWidth: .2,
                                    layout: 'form',
                                    border: false,
                                    items: [personNum]
                                }]
                        },{
                            layout: 'column',
                            border: false,
                            items: [{
                                    columnWidth: .35,
                                    layout: 'form',
                                    border: false,
                                    items: [toAirport]
                                }, {
                                    columnWidth: .2,
                                    layout: 'form',
                                    border: false,
                                    items: [searchButton]
                                }]
                        },from,to]
                });
                formPanel.render('search_div');

                function searchFlights(){
                    if (formPanel.form.isValid()) {
                        if (from.getValue() == "" || to.getValue() == "") {
                            Ext.MessageBox.alert('提示', '选择的城市有误.');
                        }else{
                            fly_date = date.getValue().format('Y-m-d');
                            fromto = from.getValue()+to.getValue();
                            airways_code = airways.getValue();
                            //alert(fly_date+"-"+fromto+"-"+airways_code);
                            //encodeURIComponent
                            ds.baseParams.seat = personNum.getValue();
                            ds.baseParams.fromto = fromto;
                            ds.baseParams.date = fly_date;
                            ds.baseParams.airways_code = airways_code;
                            ds.reload();
                        }
                    }else{
                        Ext.MessageBox.alert('错误消息', '请将必填项信息录入后再提交.');
                    }
                }

                //******************航班查询结果 airlaneGrid**********************
                var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),{
                        header: "航空公司",
                        width: 80,
                        dataIndex: 'airways_name'
                    }, {
                        header: "航班",
                        width: 70,
                        dataIndex: 'air_no'
                    }, {
                        header: "起飞时间",
                        width: 60,
                        dataIndex: 'fly_time'
                    }, {
                        header: "到达时间",
                        width: 60,
                        dataIndex: 'arr_time'
                    }, {
                        header: "机场",
                        width: 230,
                        dataIndex: 'airport'
                    }, {
                        header: "舱位",
                        width: 50,
                        dataIndex: 'position'
                    }, {
                        header: "最低价",
                        width: 50,
                        dataIndex: 'price'
                    }, {
                        header: "折扣",
                        width: 50,
                        renderer: discountRender,
                        dataIndex: 'discount'
                    }, {
                        header: "机型",
                        width: 50,
                        dataIndex: 'plane_type'
                    }, {
                        header: "返利",
                        width: 50,
                        dataIndex: 'rake_off'
                    }, {
                        header: "预定",
                        width: 50,
                        renderer: renderOption,
                        dataIndex: 'plane_type'
                    }]);
                cm.defaultSortable = true;

                var airReader = new Ext.data.JsonReader({
                    root: 'root'
                }, [{
                        name: 'airways_name'
                    }, {
                        name: 'air_no'
                    }, {
                        name: 'price'
                    }, {
                        name: 'discount'
                    }, {
                        name: 'rake_off'
                    }, {
                        name: 'airport'
                    }, {
                        name: 'fly_time'
                    }, {
                        name: 'arr_time'
                    }, {
                        name: 'plane_type'
                    }, {
                        name: 'position'
                    }]);

                var ds = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: "/ticket/searchAirLineWeb"
                    }),
                    reader: airReader
                });
                var airlaneGridpanel = new Ext.grid.GridPanel({
                    el: 'search_grid_div',
                    ds: ds,
                    cm: cm,
                    loadMask: true,
                    autoWidth: true,
                    collapsible: true,
                    animCollapse: true,
                    height: 400,
                    //frame: true,
                    title: '航班信息'
                });
                airlaneGridpanel.render();
                //ds.load();

                function discountRender(value){
                    if (value <= 70){
                        return "<span style='color:red;font-weight:bold;'>"+value+"</span>";
                    }else{
                        return value ;
                    }
                }
                var srow;
                var price = "";
                var rake_off = "";
                var air_no = "";
                var position = "";
                var fly_time = ""
                var arr_time = "";
                var plane_type = "";
                function renderOption(value, cellmeta, record, rowIndex, columnIndex, store){
                    var current_selected = ds.data.items[rowIndex].id;
                    srow = ds.getById(current_selected);
                    price = srow.get("price");
                    rake_off = srow.get("rake_off");
                    if (price == "" || rake_off == "") {
                        return "";
                    }
                    else {
                        //var str = "<input type='button' value='预定' onclick='orderTicketFun(\"" + rowIndex + "\")' />";
                        var str = "<a href=# onclick='orderTicketFun()' >预订</a>";
                        return str;
                    }
                }

                orderTicketFun = function(key){
                    air_no = srow.get("air_no");
                    position = srow.get("position");
                    plane_type = srow.get("plane_type");
                    fly_time = srow.get("fly_time");
                    arr_time = srow.get("arr_time");
                    price = srow.get("price");
                    rake_off = srow.get("rake_off");
                    if(position!=""){
                        var url = "/ticket/add_order?airNo="+air_no+"&position="+position+"&fly_date="+fly_date+"&plane_type="+plane_type+"&fly_time="+fly_time+"&arr_time="+arr_time+"&fromto="+fromto+"&price="+price+"&rake_off="+rake_off;

                        //var panel = tabs.getActiveTab();
                        //panel.getUpdater().update(url);
                        //panel.setTitle('预定航班');
                        var tabs = Ext.getCmp('tabs');
                        tabs.add({
                            id: 'tab_create_order',
                            title: '预定航班',
                            //disabled: true,
                            autoLoad: {
                                url: url,
                                scripts: true
                            },
                            closable: true
                        }).show();
                        
                    }
                }

            });
        </script>
    </body>
</html>
